/**
 * init
 */
$(function() {
	// $('input[type="checkbox"]').iCheck({checkboxClass:
	// 'icheckbox_square-green'});

	initProTable();
	
	// 查询
	$('#btnQuery').on('click', function() {
		sys.reloadTable('shareProductTable');
	});

	// 清空
	$('#btnClear').on('click', function() {
		$("#shareProductSearchForm input").val("");
		$("#isSort").val("false");
		$("#currentPage").val("1");
		sys.reloadTable('shareProductTable');
	});
});

/**
 * 初始化列表
 */
function initProTable() {
	// 定义列
	var columns = [
//			{
//				field : 'no',
//				title : '序号',
//				align : 'center',
//				valign : 'middle',
//				formatter : function(value, row, index) {
//					var options = $("#shareInteractionTable").bootstrapTable("getOptions");
//					return options.pageSize * (options.pageNumber - 1) + index + 1;
//				}
//			},
//			{
//				title : '商品图片',
//				align : 'center',
//				width : '80px',
//				formatter : function(value, row, index) {
//					var imgSrc = '';
//					if (null == row.productImagePath || '' == row.productImagePath || 'null' == row.productImagePath) {
//						imgSrc = rootPath + "/statics/images/default.jpg";
//					} else {
//						imgSrc = SystemImgPath + row.productImagePath;
//					}
//					return "<a rel='productImg' href='" + imgSrc + "'>" +
//                    		"<img src='" + imgSrc + "?x-oss-process=image/resize,m_pad,h_60,w_60' width='50px' height='60px' onerror='this.src=\"" + rootPath + "/statics/images/default.jpg\"'>" +
//                    		"</a>";
//				}
//			},
//			{
//				field : 'productCode',
//				title : '商品编码',
//				valign : 'middle',
//				align : 'center'
//			},
			{
				field : 'productName',
				title : '商品名称',
				valign : 'middle',
				width : '700px',
				align : 'left',
				formatter : function(value, row, index) {
					var imgSrc = '';
					if (null == row.productImagePath || '' == row.productImagePath || 'null' == row.productImagePath) {
						imgSrc = rootPath + "/statics/images/default.jpg";
					} else {
						imgSrc = SystemImgPath + row.productImagePath;
					}
					return "<table>" +
						"<tbody>" +
							"<tr>" +
								"<td style='width:50px;text-align:center;'>" +
									"<a rel='productImg_"+row.id+"'  href='" + imgSrc + "'>" +
										"<img src='" + imgSrc + "?x-oss-process=image/resize,m_pad,h_60,w_60' width='50px' height='60px' onerror='this.src=\"" + rootPath + "/statics/images/default.jpg\"'>" +
									"</a>" +
								"</td>" +
								"<td style='width: 400px;border-left:0px solid #fff;'>" +
									row.productName + "</br>" +
									"商品编码：" + row.productCode + "</br>" +
									row.productCategoryName +
								"</td>" +
							"</tr>" +
						"</tbody>" +
					"</table>";
				}
			},
			{
				field : 'shareQuantity',
				title : "<a name='sortBtn' tag='shareSort' href='javascript:void(0);' sort='' onclick='sortSearchBtn(this);'>" +
							"商品分享次数" +
							"<img style='padding-left:2px; padding-bottom:4px;' src='"+rootPath+"/statics/images/icon_px3.png' />" +
						"</a>",
				valign : 'middle',
				align : 'center'
			},
			{
				field : 'pvViewQuantity',
				title : "<a name='sortBtn' tag='pvViewSotr' href='javascript:void(0);' sort='' onclick='sortSearchBtn(this);'>" +
							"PV（分享）" +
							"<img style='padding-left:2px; padding-bottom:4px;' src='"+rootPath+"/statics/images/icon_px3.png' />" +
						"</a>",
				align : 'center',
				valign : 'middle'
			},
			{
				field : 'uvViewQuantity',
				title : "<a name='sortBtn' tag='uvViewSort' href='javascript:void(0);' sort='' onclick='sortSearchBtn(this);'>" +
							"UV（分享）" +
							"<img style='padding-left:2px; padding-bottom:4px;' src='"+rootPath+"/statics/images/icon_px3.png' />" +
						"</a>",
				valign : 'middle',
				align : 'center'
			},
			{
				field : 'shareOrderNum',
				title : "<a name='sortBtn' tag='orderNumSort' href='javascript:void(0);' sort='' onclick='sortSearchBtn(this);'>" +
							"销售数量（分享）" +
							"<img style='padding-left:2px; padding-bottom:4px;' src='"+rootPath+"/statics/images/icon_px3.png' />" +
						"</a>",
				valign : 'middle',
				align : 'center'
			},
			{
				field : 'shareTotalMoney',
				title : "<a name='sortBtn' tag='totalMoneySort' href='javascript:void(0);' sort='' onclick='sortSearchBtn(this);'>" +
							"销售额（分享）" +
							"<img style='padding-left:2px; padding-bottom:4px;' src='"+rootPath+"/statics/images/icon_px3.png' />" +
						"</a>",
				valign : 'middle',
				align : 'center'
			},
			{
				field : '#',
				title : '操作',
				align : 'center',
				width : '160px',
				valign : 'middle',
				formatter : function(value, row, index) {
					return "<a class='btn btn-success btn-xs' href='javaScript:void(0);' onclick='toDetail(\"" + row.id + "\")'>统计详情</a>";
				}
			}
		];

	// 定义查询回调函数（查询参数在此设置）
	var queryParams = function(params) {
		var productName = $("#productName").val();
		var productCode = $("#productCode").val();
		var shareSort = $("#shareSort").val();
		var pvViewSotr = $("#pvViewSotr").val();
		var uvViewSort = $("#uvViewSort").val();
		var orderNumSort = $("#orderNumSort").val();
		var totalMoneySort = $("#totalMoneySort").val();
		var currentPage = $("#currentPage").val();
		var isSort = $("#isSort").val();
		var page = isSort == 'false' ? params.pageNumber : currentPage;
		return {
			rows : params.limit, // 页面大小
			page : page, // 页码
			productName : productName,
			productCode : productCode,
			shareSort : shareSort,
			pvViewSotr : pvViewSotr,
			uvViewSort : uvViewSort,
			orderNumSort : orderNumSort,
			totalMoneySort : totalMoneySort,
			currentPage : currentPage,
			isSort : isSort
		};
	};

	var url = rootPath + "/share/listShareProductPage"; // 请求json数据的服务器地址
	sys.initTable('shareProductTable', {
		url : url,
		queryParams : queryParams,
		columns : columns,
		striped : true,
		clickToSelect : true,
		onLoadSuccess : function(data) {
			$("#shareSort").val(data.shareSort);
			$("#pvViewSotr").val(data.pvViewSotr);
			$("#uvViewSort").val(data.uvViewSort);
			$("#orderNumSort").val(data.orderNumSort);
			$("#totalMoneySort").val(data.totalMoneySort);
			$("#currentPage").val(data.currentPage);
			$("input[tag='sort']").each(function(){
				var this_id = $(this).attr("id");
				var sort = $(this).val();
				$("a[tag='"+this_id+"']").attr("sort",sort);
				if (sort == "ASC") {
					$("a[tag='"+this_id+"']").find("img").attr("src",rootPath+"/statics/images/ASC.png");
					$("a[tag='"+this_id+"']").css("color","#FF0000");
				} else if (sort == "DESC"){
					$("a[tag='"+this_id+"']").find("img").attr("src",rootPath+"/statics/images/DESC.png");
					$("a[tag='"+this_id+"']").css("color","#FF0000");
				} else {
					$("a[tag='"+this_id+"']").find("img").attr("src",rootPath+"/statics/images/icon_px3.png");
					$("a[tag='"+this_id+"']").css("color","#5A738E");
				}
			});
			$("#isSort").val("false");
			initBootstrapTableCheckbox();
			initFancyBoxProductImg("productImg");
		}
	});
}

//排序查询
function sortSearchBtn(this_){
	var tag = $(this_).attr("tag");
	var sort = $(this_).attr("sort");
	if (!sort) {
		sort = "ASC";
	} else if (sort == "ASC") {
		sort = "DESC";
	} else {
		sort = "";
	}
	$("#"+tag).val(sort);
//	$("#isSort").val("true");
	sys.reloadTable('shareProductTable');
}

//加载图片浏览器
function initFancyBoxProductImg(relName){
	$("a[rel^='"+relName+"']").each(function(){
		$(this).fancybox({
			'titlePosition' : 'over',
			'cyclic'        : false,
			'titleFormat'	: function(title) {
				return '';
			}
		});
	});
}

//跳转详情页面
function toDetail(id) {
	window.location.href = rootPath + "/share/toDetailPage?id="+id;
}